package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.SeckillModel;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.ui.packagecardproject.PushPackageCardDetailActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

/**
 * Created by asus on 2017/5/18.
 */
public class SeckillAdapter extends RecyclerArrayAdapter<SeckillModel> {
    private Context context;
    private int type;
    private static final int SEC_KILL_TYPE = 1;
    private static final int NOT_START_TYPE = 0;
    private static final int IN_SALE_TYPE = 1;
    private static final int SALE_FINISH_TYPE = 2;
    private static final int SALE_OUT_TYPE = 3;

    public SeckillAdapter(Context context, int type) {
        super(context);
        this.context = context;
        this.type = type;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new SeckillHolder(parent, viewType);
    }

    @Override
    public int getViewType(int position) {
        Long currentTime = getAllData().get(position).getTimestamp();
        if (currentTime < getAllData().get(position).getStartTime()) {
            return NOT_START_TYPE;
        } else if (currentTime > getAllData().get(position).getStopTime()) {
            return SALE_FINISH_TYPE;
        } else {
            if (getAllData().get(position).getCurrentCount() == 0) {
                return SALE_OUT_TYPE;
            } else {
                return IN_SALE_TYPE;
            }

        }

    }

    class SeckillHolder extends BaseViewHolder<SeckillModel> {
        private ImageView ivGoodsImg;
        private TextView tvGoodsName;
        private TextView tvSecPrice;
        private TextView tvSpecPrice;
        private TextView tvGoodsBuy;
        private ProgressBar pbGoodsBuy;
        private TextView tvGoodsSign;
        private int viewType;

        public SeckillHolder(ViewGroup parent, int viewType) {
            super(parent, R.layout.item_seckill_recycler);
            ivGoodsImg = $(R.id.iv_goods_img);
            tvGoodsName = $(R.id.tv_goods_name);
            tvSecPrice = $(R.id.tv_sec_price);
            tvSpecPrice = $(R.id.tv_spec_price);
            tvGoodsBuy = $(R.id.tv_goods_buy);
            pbGoodsBuy = $(R.id.pb_goods_buy);
            tvGoodsSign = $(R.id.tv_goods_sign);
            this.viewType = viewType;
        }

        @Override
        public void setData(final SeckillModel model) {
            super.setData(model);
            GlideUtils.loadImage(context, model.getLogo(), ivGoodsImg, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
            tvGoodsName.setText(model.getGoodsName());
            tvSecPrice.setText(String.format("%.2f", model.getKillPrice()));
            tvSpecPrice.setText("￥" + String.format("%.2f", model.getGoodsSpecValuesPrice()));
            tvSpecPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            if (type == 0) {
                tvGoodsBuy.setText("已抢" + (int) (((model.getTotalCount() - model.getCurrentCount()) / model.getTotalCount()) * 100)+ "%");
                pbGoodsBuy.setProgress((int) (((model.getTotalCount() - model.getCurrentCount()) / model.getTotalCount()) * 100));
                switch (viewType) {
                    case NOT_START_TYPE:
                        tvGoodsBuy.setVisibility(View.INVISIBLE);
                        pbGoodsBuy.setVisibility(View.INVISIBLE);
                        tvGoodsSign.setBackgroundResource(R.drawable.shape_seckill_app);
                        tvGoodsSign.setText("未开始");
                        break;
                    case IN_SALE_TYPE:
                        tvGoodsSign.setBackgroundResource(R.drawable.shape_seckill_yellow);
                        tvGoodsSign.setText("马上抢");
                        break;
                    case SALE_FINISH_TYPE:
                        tvGoodsSign.setBackgroundResource(R.drawable.shape_seckill_gray);
                        tvGoodsSign.setText("已结束");
                        break;
                    case SALE_OUT_TYPE:
                        tvGoodsSign.setBackgroundResource(R.drawable.shape_seckill_gray);
                        tvGoodsSign.setText("已抢光");
                        break;

                }
            } else {
                tvGoodsBuy.setVisibility(View.INVISIBLE);
                pbGoodsBuy.setVisibility(View.INVISIBLE);
                tvGoodsSign.setBackgroundResource(R.drawable.shape_seckill_app);
                tvGoodsSign.setText("未开始");
            }

        }
    }
}
